package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.dao.StudentDao;
import com.qf.entity.TbStudent;
import com.qf.entity.TbTeacher;
import com.qf.service.StudentService;
import com.qf.utils.ResultMsg;
import com.qf.utils.SessionKey;
import com.qf.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 王希卓
 * @Date 2023/7/15 16:54
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentDao, TbStudent> implements StudentService {

    /**
     * 分页模糊查询所有学生
     * @param current 当前页码
     * @param size 每页显示条数
     * @param keyword 关键字
     * @return
     */
    @Override
    public ResultMsg findAll(int current, int size, String keyword) {
        QueryWrapper<TbStudent> wrapper = new QueryWrapper<>();
        wrapper.like("student_name",keyword);
        Page page = new Page(current, size);
        Page page1 = this.page(page, wrapper);
        return ResultMsg.success(page1);
    }

    /**
     * 添加或修改学生
     * @param tbStudent 学生实体
     * @return
     */
    @Override
    public ResultMsg update(TbStudent tbStudent) {
        this.saveOrUpdate(tbStudent);
        return ResultMsg.success();
    }

    /**
     * 根据学生id删除
     * @param studentId
     * @return
     */
    @Override
    public ResultMsg del(int studentId) {
        this.removeById(studentId);
        return ResultMsg.success();
    }

    @Override
    public ResultMsg studentlogin(String name, String password, HttpServletRequest request) {
        QueryWrapper<TbStudent> wrapper = new QueryWrapper<>();
        wrapper.eq("student_name", name);
        TbStudent student = this.getOne(wrapper);

        if (student == null)
            return ResultMsg.faild(SessionKey.USER_EXITE, "学生不存在");

        boolean e = student.getStudentPass().equals(password);
        //判断密码
        if (e) {
            String jwt = TokenUtils.create(String.valueOf(student.getStudentId()), student.getStudentPass());
            return ResultMsg.success(jwt);
        }

        return null;
    }
}
